package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.ActivityCollector;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainCashierActivity extends BaseActivity {


    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.tv_transfer)
    TextView tvTransfer;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.tv_his)
    TextView tvHis;
    private boolean isMember;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_cashier);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(MainCashierActivity.this, Constant.USER_ID, "");
        initView();
        getDatas();
    }

    private void getDatas() {
        DialogUtils.showProgressDlg(this, "");
        OkGo.<String>post(Constant.PAY_QRCODE)
                .tag(this)
                .params("user_id", userId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
//                        String res = response.body().toString();
//                        BaseBean<StatusBean> bean = new Gson().fromJson(res, new TypeToken<BaseBean<StatusBean>>() {
//                        }.getType());
//                        if (Constant.SUCESS == bean.getStatus()) {
//                            Glide.with(MainCashierActivity.this).load(bean.getData().get(0).getPay_qrcode_img()).diskCacheStrategy(DiskCacheStrategy.NONE).into(imgCode);
//                        } else {
//                            ToastUtils.toastForLong(MainCashierActivity.this, bean.getMsg());
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        super.onError(response);
                        ToastUtils.toastForShort(MainCashierActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        tvMiddleTitle.setText("收款");
        titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("退出");
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.tv_transfer, R.id.tv_withdraw,R.id.tv_his})
    public void onViewClicked(View view) {
        Intent i = new Intent(MainCashierActivity.this, WithDrawalCashierActivity.class);
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                break;
            case R.id.title_right_one_tv:
                logOut();
                break;
            case R.id.tv_transfer:
                i.putExtra("type", Constant.ACCOUNT_Z);
                startActivity(i);
                break;
            case R.id.tv_withdraw:
                i.putExtra("type", Constant.ACCOUNT_T);
                startActivity(i);
                break;
            case R.id.tv_his:
                Intent i1=new Intent();
                i1.setClass(MainCashierActivity.this,CashierHisActivity.class);
                startActivity(i1);
                break;
        }
    }

    private boolean isRemember;
    private String name;
    private String psw;
    NormalDialog dialog;

    /**
     * 退出登录
     */
    private void logOut() {
        dialog = DialogUtils.getNormalDialog(MainCashierActivity.this, "提示", "是否退出登录?");
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                isRemember = (boolean) SPUtils.get(MainCashierActivity.this, Constant.IS_REMEMBER, false);
                isMember = (boolean) SPUtils.get(MainCashierActivity.this, Constant.IS_MEMBER, false);
                name = (String) SPUtils.get(MainCashierActivity.this, Constant.LOGIN_NAME, "");
                psw = (String) SPUtils.get(MainCashierActivity.this, Constant.LOGIN_PSW, "");
                SPUtils.clear(MainCashierActivity.this);
                SPUtils.put(MainCashierActivity.this, Constant.isLogin, false);
                SPUtils.put(MainCashierActivity.this, Constant.IS_REMEMBER, isRemember);
                SPUtils.put(MainCashierActivity.this, Constant.LOGIN_NAME, name);
                SPUtils.put(MainCashierActivity.this, Constant.LOGIN_PSW, psw);
                SPUtils.put(MainCashierActivity.this, Constant.IS_MEMBER, isMember);
                Intent intent = new Intent(MainCashierActivity.this, LoginActivity.class);
                startActivity(intent);
                ActivityCollector.getInstances().finishAllActivity();
//                    MainCashierActivity.this.finish();
            }
        });
    }

}
